//
//  GroupTypes+GroupTypes.m
//  ClinicalCollections
//
//  Created by janakiraman gopinath on 2/10/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "GroupTypes+GroupTypes.h"

@implementation GroupTypes (GroupTypes)

+ (void) groupTypes:(NSString *) type
               usingResults:results
      usingClinicalTrialGov:(ClinicalTrialGov *) clinicalTrialGov
               usingContext:(NSManagedObjectContext *) context
{
    // Multiple outomes in  outcomelist 
    if ([type isEqual:@"outcome_list"]) {
        NSArray *outcomes = [results valueForKeyPath:@"clinical_results.outcome_list.outcome"];
        
        for (int i = 0; i < outcomes.count; i++)
        {
            GroupTypes *groupTypes;
            groupTypes = (GroupTypes *)[NSEntityDescription insertNewObjectForEntityForName:@"GroupTypes"
                                                                     inManagedObjectContext:context];
            groupTypes.type = [NSString stringWithFormat:@"outcome %d" , i];
            
            if (![[outcomes[i] valueForKey:@"description"] isEqual:[NSNull null]])
                groupTypes.desc = [outcomes[i] valueForKey:@"description"];
            if (![[outcomes[i] valueForKey:@"title"] isEqual:[NSNull null]])
                groupTypes.title = [outcomes[i] valueForKeyPath:@"title"];
            if (![[outcomes[i] valueForKey:@"time_frame"] isEqual:[NSNull null]])
                groupTypes.timeFrame  = [outcomes[i] valueForKeyPath:@"time_frame"];
            if (![[outcomes[i] valueForKey:@"safety_issue"] isEqual:[NSNull null]])
                groupTypes.safetyIssue = [outcomes[i] valueForKeyPath:@"safety_issue"];
            if (![[outcomes[i] valueForKey:@"type"] isEqual:[NSNull null]])
                groupTypes.subType = [outcomes[i] valueForKeyPath:@"type"];
            groupTypes.clinicalTrialGov = clinicalTrialGov;
            
            // Load up group_type = whatever
            NSString *formatString = [NSString stringWithFormat:@"group_list.group"];
            NSArray *groupList = [outcomes[i] valueForKeyPath:formatString];
            
            [[Groups class] groups:groupList
                          withType:type
                   usingGroupTypes:groupTypes
             usingClinicalTrialGov:clinicalTrialGov
                      usingContext:context
                      usingResults:results];
            
            NSMutableArray *measures;
            measures = [outcomes[i] valueForKeyPath:@"measure_list.measure"];
            if (measures && ![measures isEqual:[NSNull null]]) {
                for (int i = 0; i < measures.count; i++)
                    [[Measure class] measure:measures[i]
                                    withType:type
                             usingGroupTypes:groupTypes
                       usingClinicalTrialGov:clinicalTrialGov
                                usingContext:context];
            }
    ;
        }
    }
    else {
        GroupTypes *groupTypes;
        groupTypes = (GroupTypes *)[NSEntityDescription insertNewObjectForEntityForName:@"GroupTypes"
                                                                 inManagedObjectContext:context];
        groupTypes.type = type;
        groupTypes.clinicalTrialGov = clinicalTrialGov;
        
        // Load up group_type = whatever
        NSString *formatString = [NSString stringWithFormat:@"clinical_results.%@.group_list.group", type];
        NSArray *groupList = [results valueForKeyPath:formatString];
        
        [[Groups class] groups:groupList
                      withType:type
               usingGroupTypes:groupTypes
         usingClinicalTrialGov:clinicalTrialGov
                  usingContext:context
                  usingResults:results];
        
        NSMutableArray *measures;
        if ([type isEqual:@"baseline"]) {
            measures = [results valueForKeyPath:@"clinical_results.baseline.measure_list.measure"];
        }
        else if ([type isEqual:@"participant_flow"]) {
            measures = [results valueForKeyPath:@"clinical_results.participant_flow.period_list.period"];
        }
        
        //NSLog(@"Group Type Inserted %@" , groupTypes);
        if (measures && ![measures isEqual:[NSNull null]]) {
            for (int i = 0; i < measures.count; i++)
                [[Measure class] measure:measures[i]
                                withType:type
                         usingGroupTypes:groupTypes
                   usingClinicalTrialGov:clinicalTrialGov
                            usingContext:context];
        }
    }
}

@end
